import React, { Component } from "react";
import Board from "./Board";
import "./App.css";
function calculateWinner(squares) {
  const lines = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];
  for (let i = 0; i < lines.length; i++) {
    const [a, b, c] = lines[i];
    if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
      console.log(lines[i], "winner");
      return [squares[a], lines[i]];
    }
  }
  return [null];
}
class Game extends Component {
  constructor(props) {
    super(props);
    this.state = {
      history: [
        {
          squares: Array(9).fill(null),
          posIdx: 0,
        },
      ],
      xIsNext: true,
      stepNumber: 0,
      posIdx: 0,
      selected: Array(9).fill(false),
      historyOrder: true,
      winnerlist: Array(9).fill(false),
      lastWinnerlist: [],
      winnerStepNumber: 0,
    };
  }
  handleClick(i) {
    // slice不改变源数组，复制出个新数组
    const history = this.state.history.slice(0, this.state.stepNumber + 1);
    const current = history[history.length - 1];
    const squares = current.squares.slice();
    if (calculateWinner(squares)[0] || squares[i]) {
      alert("winner", this.state.xIsNext);
      return;
    }
    squares[i] = this.state.xIsNext ? "X" : "O";
    let newSelected = Array(9).fill(false);
    newSelected[i] = true;
    this.setState({
      history: history.concat([
        {
          squares: squares,
          posIdx: i,
        },
      ]),
      xIsNext: !this.state.xIsNext,
      stepNumber: history.length,
      posIdx: i,
      selected: newSelected,
    });
    if (this.state.stepNumber === 8 && !calculateWinner(squares)[0]) {
      console.log("平局");
    }
    if (calculateWinner(squares)[1]) {
      let newWinner = Array(9).fill(false);
      calculateWinner(squares)[1].forEach((list) => {
        newWinner[list] = true;
      });
      this.setState({
        winnerlist: newWinner,
        lastWinnerlist: newWinner,
        winnerStepNumber: this.state.stepNumber,
      });
    }
  }
  jumpTo(step) {
    let posIdx = this.state.history[step].posIdx;
    let newSelected = Array(9).fill(false);
    newSelected[posIdx] = true;
    if (step === this.state.winnerStepNumber + 1) {
      this.setState({
        stepNumber: step,
        xIsNext: step % 2 === 0,
        posIdx: posIdx,
        selected: newSelected,
        winnerlist: this.state.lastWinnerlist,
      });
    } else {
      this.setState({
        stepNumber: step,
        xIsNext: step % 2 === 0,
        posIdx: posIdx,
        selected: newSelected,
        winnerlist: Array(9).fill(false),
      });
    }
  }
  handleOrder(flag) {
    this.setState({
      historyOrder: flag,
    });
  }
  render() {
    const { history } = this.state;
    const current = history[this.state.stepNumber];
    // const current = history[history.length - 1];
    const winner = calculateWinner(current.squares)[0];
    const moves = history.map((step, move) => {
      let index = this.state.historyOrder ? move : history.length - 1 - move;
      const desc = index ? "Go to move #" + index : "Go to game start";
      return (
        <li key={index}>
          <button onClick={() => this.jumpTo(index)}>{desc}</button>
        </li>
      );
    });
    let status;
    if (winner) {
      status = "Winner: " + winner;
    } else {
      status =
        "Next player:* " +
        (this.state.xIsNext ? "X" : "O") +
        "- x坐标:" +
        (this.state.posIdx % 3) +
        "- y坐标:" +
        Math.floor(this.state.posIdx / 3);
    }
    return (
      <div className="game">
        <Board
          squares={current.squares}
          selected={this.state.selected}
          winnerlist={this.state.winnerlist}
          onClick={(i) => this.handleClick(i)}
        ></Board>
        <button onClick={() => this.handleOrder(!this.state.historyOrder)}>{`${
          this.state.historyOrder ? "升序" : "降序"
        }`}</button>
        <div className="game-info">
          <div>{status}</div>
          <ol>{moves}</ol>
        </div>
      </div>
    );
  }
}

export default Game;
